import config
import requests
import json
import time

def git_request(url,param):
    header = {
        'Authorization': 'token {}'.format(config.get_config()['token'])
    }
    return requests.get(url,params=param,headers=header)

def get_commmit_data(commit_url,start_time,stop_time):
    i = 1
    api_data=[]
    while(True):
        param = {
            'since': start_time,
            'until': stop_time,
            'per_page': 100 ,
            'page': i
        }
        new_data = json.loads(git_request(commit_url, param).text)
        if(len(new_data)<1):
            break;
        api_data.extend(new_data)
        i += 1
    return api_data

def get_commmitcomment_data(commitcomment_url):
    i = 1
    api_data=[]
    while(True):
        param = {
            'per_page': 100 ,
            'page': i
        }
        new_data = json.loads(git_request(commitcomment_url, param).text)
        if(len(new_data)<1):
            break;
        api_data.extend(new_data)
        i += 1
    return api_data

def get_issue_data(issue_url,start_time):
    i = 1
    api_data=[]
    while(True):
        param = {
            'since': start_time,
            'per_page': 100,
            'page': i
        }
        new_data = json.loads(git_request(issue_url, param).text)
        if(len(new_data)<1):
            break;
        api_data.extend(new_data)
        i += 1
    return api_data

def get_issuecomment_data(issuecomment_url,start_time):
    i = 1
    api_data=[]
    while(True):
        param = {
            'since': start_time,
            'per_page': 100,
            'page': i
        }
        new_data = json.loads(git_request(issuecomment_url, param).text)
        if(len(new_data)<1):
            break;
        api_data.extend(new_data)
        i += 1
    return api_data

def get_PR_data(PR_url,start_time,stop_time):
    i = 1
    api_data=[]
    start_time = time.strptime(start_time, config.get_config()['FMT']['date'])
    while(True):
        param = {
            'per_page': 100 ,
            'page': i
        }
        new_data = json.loads(git_request(PR_url, param).text)
        early_time = time.strptime(new_data[-1]['created_at'], config.get_config()['FMT']['date'])
        diff = time.mktime(early_time)-time.mktime(start_time)
        if(diff<0):
            break;
        api_data.extend(new_data)
        i += 1
    return api_data

# def get_follow_info(url):
#     user_data = json.loads(git_request(url, {}).text)
#     return user_data['followers'], user_data['following']
